import { Component, OnInit, Input } from '@angular/core'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { ETradeChannel, ETransScene, TransProc } from '@shared/enums'
import { I18NService } from '@core'
import { Router, ActivatedRoute } from '@angular/router'
import { TitleService } from '@delon/theme'
import { PageService } from '@shared/services/page.service'
import { DetailBaseClass } from '@shared/classes/DetailBaseClass'
import { ITrade } from '@shared/interfaces/trade'
import { AmtFormatPipe } from '@shared/filters/amt-format.pipe'

@Component({
  selector: 'app-charge-back-origin-trade-info',
  templateUrl: './origin-trade-detail.component.html',
})
export class OriginTradeInfoComponent extends DetailBaseClass<ITrade> implements OnInit {
  firstColumns: IBaseInfoColumn[]
  secondColumns: IBaseInfoColumn[]
  thirdColumns: IBaseInfoColumn[]

  acquirerColumns: IBaseInfoColumn[]
  groupColumns: IBaseInfoColumn[]
  merchantColumns: IBaseInfoColumn[]
  storeColumns: IBaseInfoColumn[]

  /** 可退款金额 */
  availableRefundAmt: number
  @Input() trade: ITrade

  constructor(
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public titleService: TitleService,
    public pageService: PageService,
    private amtFormatPipe: AmtFormatPipe,
  ) {
    super(pageService)
  }

  ngOnInit() {
    this.detailData = this.trade
    this.initFirstColumns()
    this.initSecondColumns()
    this.initThirdColumns()
    this.initAcquirerColumns()
    this.initGroupColumns()
    this.initMerchantColumns()
    this.initStoreColumns()
    this.initColumns()
  }

  initColumns(): void {
    this.initValue.call(this, this.firstColumns)
    this.initValue.call(this, this.secondColumns)
    this.initValue.call(this, this.thirdColumns)
    this.initValue.call(this, this.acquirerColumns)
    this.initValue.call(this, this.groupColumns)
    this.initValue.call(this, this.merchantColumns)
    this.initValue.call(this, this.storeColumns)
  }

  initFirstColumns(): void {
    const { transAmt, currency } = this.detailData
    this.firstColumns = [
      {
        span: 8,
        index: 'createdAt',
        label: '交易时间',
        type: 'date',
      },
      {
        span: 8,
        index: 'orderNum',
        label: '订单号',
      },
      {
        span: 8,
        index: 'transAmt',
        label: '交易金额',
        asyncValue: this.amtFormatPipe.transform(transAmt, currency),
      },
    ]
  }

  initSecondColumns(): void {
    this.secondColumns = [
      {
        span: 8,
        index: 'paymentBrand',
        label: '渠道',
        pipe: this.parsePaymentBrand.bind(this),
      },
      {
        span: 8,
        index: 'transScene',
        label: '交易场景',
        pipe: this.parseTransScene.bind(this),
      },
      {
        span: 8,
        index: '',
        label: '渠道订单号',
      },
    ]
  }

  initThirdColumns(): void {
    this.thirdColumns = [
      {
        span: 8,
        label: '检索参考号',
        labelI18n: 'OPERATION.TRADE.RETRIEVAL_REFNUM',
        index: 'retrievalRefNum',
      },
      {
        span: 8,
        label: '卡号',
        labelI18n: 'TRADE.CARD_NUMBER',
        index: 'acctNumSensitive',
      },
      {
        span: 8,
        label: '授权码',
        labelI18n: 'TRADE.AUTH_CODE',
        index: 'authCode',
      },
    ]
  }

  initAcquirerColumns(): void {
    this.acquirerColumns = [
      {
        span: 8,
        index: 'insCode',
        label: '机构编号',
      },
      {
        span: 8,
        index: 'insName',
        label: '机构名称',
      },
      {
        span: 8,
        index: 'insNameEN',
        label: '机构英文名称',
      },
    ]
  }

  initGroupColumns(): void {
    this.groupColumns = [
      {
        span: 8,
        index: 'agentCode',
        label: '集团编号',
      },
      {
        span: 8,
        index: 'agentName',
        label: '集团名称',
      },
      {
        span: 8,
        index: 'agentNameEN',
        label: '集团英文名称',
      },
    ]
  }

  initMerchantColumns(): void {
    this.merchantColumns = [
      {
        span: 8,
        index: 'merNum',
        label: '商户编号',
      },
      {
        span: 8,
        index: 'merName',
        label: '商户名称',
      },
      {
        span: 8,
        index: 'merNameEN',
        label: '商户英文名称',
      },
    ]
  }

  initStoreColumns(): void {
    this.storeColumns = [
      {
        span: 8,
        index: 'storeNum',
        label: '门店编号',
      },
      {
        span: 8,
        index: 'storeName',
        label: '门店名称',
      },
      {
        span: 8,
        index: 'storeNameEN',
        label: '门店英文名称',
      },
    ]
  }

  parsePaymentBrand(paymentBrand: string): string {
    try {
      return this.trans(ETradeChannel[paymentBrand])
    } catch (e) {
      return this.trans('GENERAL.INFO.NONE')
    }
  }

  parseTransScene(transScene: string): string {
    try {
      return this.trans(ETransScene[transScene])
    } catch (e) {
      return this.trans('GENERAL.INFO.NONE')
    }
  }

  parseProcFlag(procFlag: string): string {
    return TransProc[procFlag]
  }

  trans(str: string): string {
    return this.i18n.fanyi(str)
  }
}
